#include <Adafruit_NeoPixel.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

#define wifi_ssid "HUAWEI-354"
#define wifi_password "88888888"

#define mqtt_server "espush.cn"
#define mqtt_port 1883
#define mqtt_user "100555"
#define mqtt_password "8033fe7bc410658a0fa1025d71640a03"

#define in_topic "led"
#define out_topic "led"

#define in_led D4

#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
 
// 控制 WS2812 灯条的引脚编号
#define PIN        D3
 
//定义控制的 LED 数量
#define NUMPIXELS 8
 
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
 
//相邻 LED 之间的延迟，单位毫秒
#define DELAYVAL 500

WiFiClient espClient;
PubSubClient client;

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setClient(espClient);
  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);
  
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(in_led, OUTPUT);
  digitalWrite(in_led, HIGH);
  
  #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
  #endif
  // END of Trinket-specific code.
 
  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
}

void setup_wifi() {
  delay(100);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);

  //WiFi.begin(wifi_ssid, wifi_password);
  Serial.print("Success.");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    // If you do not want to use a username and password, change next line to
    // if (client.connect("ESP8266Client")) {
    if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void callback(char* topic, byte* payload, unsigned int length) {
 Serial.print("Message arrived [");
 Serial.print(topic);
 Serial.print("] ");
 for (int i = 0; i < length; i++) {
  char receivedChar = (char)payload[i];
  Serial.print(receivedChar);
  pixels.clear(); 
  if (receivedChar == '0'){
    //digitalWrite(in_led, LOW);
     for(int i=0; i<NUMPIXELS; i++) 
      pixels.setPixelColor(i, pixels.Color(0, 0, 150));
    
  }
  if (receivedChar == '1'){
    //digitalWrite(in_led, HIGH);  
    for(int i=0; i<NUMPIXELS; i++) 
      pixels.setPixelColor(i, pixels.Color(0, 150, 0));     
  }
  if (receivedChar == '2'){
    //digitalWrite(in_led, HIGH);  
    for(int i=0; i<NUMPIXELS; i++) 
      pixels.setPixelColor(i, pixels.Color(150, 0, 0));     
  }
  if (receivedChar == '3'){
    //digitalWrite(in_led, HIGH);  
    for(int i=0; i<NUMPIXELS; i++) 
      pixels.setPixelColor(i, pixels.Color(0, 255, 255));     
  }
  if (receivedChar == '4'){
    //digitalWrite(in_led, HIGH);  
    for(int i=0; i<NUMPIXELS; i++) 
      pixels.setPixelColor(i, pixels.Color(255, 255, 0));     
  }
   if (receivedChar == '5'){
    //digitalWrite(in_led, HIGH);  
    for(int i=0; i<NUMPIXELS; i++) 
      pixels.setPixelColor(i, pixels.Color(255, 0, 250));     
  }
   pixels.show();
 }
 Serial.println();
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  //client.publish(out_topic, String(random(2)).c_str(), true);
  //delay(1000);
  client.subscribe(in_topic);
  //delay(1000);

}
